﻿using System;
using System.Windows;
using System.Windows.Controls;
using MDCAdminTool.Controls;

namespace MDCAdminTool
{
    public partial class App : Application
    {
        protected override void OnExit( ExitEventArgs e )
        {
            var clusters = this.Properties["Clusters"] as Clusters;

            if ( clusters != null )
            {
                foreach (var cluster in clusters)
                {
                    cluster.Dispose();
                }
            }
        }

        public static Clusters Clusters
        {
            get { return Current.Properties["Clusters"] as Clusters; }
            set { Current.Properties["Clusters"] = value; }
        }
        public static TreeView Tree
        {
            get { return Current.Properties["ClustersTreeView"] as TreeView; }
            set { Current.Properties["ClustersTreeView"] = value; }
        }
        public static ListBox Output
        {
            get { return Current.Properties["Output"] as ListBox; }
            set { Current.Properties["Output"] = value; }
        }
        public static TabControl TabControl
        {
            get { return Current.Properties["TabControl"] as TabControl; }
            set { Current.Properties["TabControl"] = value; }
        }
        public static AsyncProgressBar ProgressBar
        {
            get { return Current.Properties["PowerShellProgressBar"] as AsyncProgressBar; }
            set { Current.Properties["PowerShellProgressBar"] = value; }
        }

        public static DataTemplate CacheClusterContentTemplate
        {
            get { return Current.Resources["CacheClusterContentTemplate"] as DataTemplate; }
            set { Current.Resources["CacheClusterContentTemplate"] = value; }
        }
        public static DataTemplate CacheHostContentTemplate
        {
            get { return Current.Resources["CacheHostContentTemplate"] as DataTemplate; }
            set { Current.Resources["CacheHostContentTemplate"] = value; }
        }
        public static DataTemplate CacheNameContentTemplate
        {
            get { return Current.Resources["CacheNameContentTemplate"] as DataTemplate; }
            set { Current.Resources["CacheNameContentTemplate"] = value; }
        }
        public static DataTemplate CacheRegionContentTemplate
        {
            get { return Current.Resources["CacheRegionContentTemplate"] as DataTemplate; }
            set { Current.Resources["CacheRegionContentTemplate"] = value; }
        }

        public static DataTemplate SelectDataTemplate( object item )
        {
            DataTemplate result = null;

            if ( item is CacheCluster )
                result = App.CacheClusterContentTemplate;

            if ( item is CacheHost )
                result = App.CacheHostContentTemplate;

            if ( item is MdcCache )
                result = App.CacheNameContentTemplate;

            if ( item is CacheRegion )
                result = App.CacheRegionContentTemplate;


            return result;
        }
        public static void NewTab()
        {
            NewTab( Tree.SelectedItem );
        }
        public static void NewTab(object item)
        {
            Application.Current.Dispatcher.Invoke(
                new Action(
                    () =>
                        {
                            try
                            {
                                var tv = Tree;

                                if (tv == null) return;

                                var dt = App.SelectDataTemplate(item);

                                if (dt == null) return;

                                var tab =
                                    new TabItem
                                        {
                                            Header = tv.SelectedItem,
                                            Content = item,
                                            ContentTemplate = dt
                                        };

                                TabControl.Items.Add(tab);
                                TabControl.SelectedItem = tab;
                                tab.Focus();
                            }
                            catch (Exception ex)
                            {
                                Output.Items.Add(ex.Message);
                            }
                        }));
        }

        public static void ActionOnUIThread( Action action )
        {
            Current.Dispatcher.Invoke(action);
        }
    }
}
